﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TZ.PlugIn.FileSrv.CustomApi
{
    public class ResponseParser
    {
        /// <summary>
        /// 解析自定义接口返回是否成功
        /// </summary>
        /// <param name="jsonResponse">JSON响应字符串</param>
        /// <returns>是否成功</returns>
        public static bool IsSuccess(string jsonResponse)
        {
            try
            {
                var response = JsonConvert.DeserializeObject<CustomApiResponse>(jsonResponse);
                return response?.Result?.ResponseStatus?.IsSuccess == true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 完整解析响应信息
        /// </summary>
        /// <param name="jsonResponse">JSON响应字符串</param>
        /// <returns>解析后的响应对象</returns>
        public static CustomApiResponse ParseResponse(string jsonResponse)
        {
            try
            {
                return JsonConvert.DeserializeObject<CustomApiResponse>(jsonResponse);
            }
            catch (Exception ex)
            {
                throw new Exception($"解析响应失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取成功实体信息
        /// </summary>
        /// <param name="jsonResponse">JSON响应字符串</param>
        /// <returns>成功实体列表</returns>
        public static List<SuccessEntity> GetSuccessEntities(string jsonResponse)
        {
            var response = ParseResponse(jsonResponse);
            return response?.Result?.ResponseStatus?.SuccessEntitys ?? new List<SuccessEntity>();
        }

        /// <summary>
        /// 获取错误信息
        /// </summary>
        /// <param name="jsonResponse">JSON响应字符串</param>
        /// <returns>错误信息列表</returns>
        public static List<string> GetErrors(string jsonResponse)
        {
            var response = ParseResponse(jsonResponse);
            var errors = new List<string>();

            if (response?.Result?.ResponseStatus?.Errors != null)
            {
                foreach (var error in response.Result.ResponseStatus.Errors)
                {
                    errors.Add(error?.ToString() ?? "");
                }
            }

            return errors;
        }
    }
    public class Result
    {
        [JsonProperty("ResponseStatus")]
        public ResponseStatus ResponseStatus { get; set; }

        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("Number")]
        public string Number { get; set; }

        [JsonProperty("NeedReturnData")]
        public List<object> NeedReturnData { get; set; }
    }
    // 完整响应DTO
    public class CustomApiResponse
    {
        [JsonProperty("Result")]
        public Result Result { get; set; }
    }
    // 响应状态DTO
    public class ResponseStatus
    {
        [JsonProperty("IsSuccess")]
        public bool IsSuccess { get; set; }

        [JsonProperty("Errors")]
        public List<object> Errors { get; set; }

        [JsonProperty("SuccessEntitys")]
        public List<SuccessEntity> SuccessEntitys { get; set; }

        [JsonProperty("SuccessMessages")]
        public List<object> SuccessMessages { get; set; }

        [JsonProperty("MsgCode")]
        public int MsgCode { get; set; }
    }
    // 成功实体DTO
    public class SuccessEntity
    {
        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("Number")]
        public string Number { get; set; }

        [JsonProperty("DIndex")]
        public int DIndex { get; set; }
    }
}
